package com.plian.system.service.sys.bigdata.Property;


import com.alibaba.fastjson.JSONObject;
import com.plian.Tools.UuidUtil;
import com.plian.Tools.pagedata.PageData;
import com.plian.system.entity.pm.propertyinformation.PropertyTime;
import com.plian.system.mapper.fr.bigdata.PropertyinfoMapper;
import com.plian.system.mapper.pm.propertyInfo.PropertyInfoMapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author huzb
 * @create 2020-10-23 9:02
 */
@Service
@AllArgsConstructor
public class PropertyinfoService {

    private final PropertyinfoMapper propertyinfoMapper;

    private final PropertyInfoMapper infoMapper;

    public List<PageData>  insertBasicInfo(PropertyTime propertyTime){
        String op = "0";
        List<PageData> list = new ArrayList<>();
        PageData pageData = new PageData();
        com.alibaba.fastjson.JSONObject jsonObject = JSONObject.parseObject(propertyTime.toString());
        String starttime = jsonObject.getString("startTime");
        String endtime = jsonObject.getString("endTime");
        pageData.put("startTime",starttime);
        pageData.put("endTime",endtime);
        List<PageData>  listByDateRange = infoMapper.selectToPd(pageData);
        addList(listByDateRange,list,op);
        return  listByDateRange;
    }

//    @DS("slave_1")
    public void insertPropertyInfo(List<PageData> list){
        for (PageData pd :list){
            propertyinfoMapper.insertpropertyInfo(pd);
        }
    }

    public  void addList(List<PageData> listByDateRange,List<PageData> list,String op){
        List<PageData> codeByName = infoMapper.codeByName();
        for (PageData pd : listByDateRange){
            Set set = pd.keySet();
            Iterator iterator = set.iterator();
            while (iterator.hasNext()){
                String key = (String)iterator.next();
                String value = getName(key,pd,codeByName);
                if (value == null){
                    pd.put(key,"");
                }else {
                    pd.put(key,value);
                }
            }
            pd.put("id", UuidUtil.get32UUID());
            pd.put("op",op);
            list.add(pd);
        }
    }

    public String getName(String key,PageData pd,List<PageData> codeByName){
        String value = pd.getString(key);
        if ("chanquandengjiqingku".equals(key) || "suoshubumen".equals(key) || "qiyeleibie".equals(key) || "guozijianguanjigou".equals(key) || "zuzhixingshi".equals(key) || "qiyejici".equals(key)
                || "jingyingzhuangkuang".equals(key) ||"bizhong".equals(key)
        ){
            for (PageData pageData:codeByName){
                if (pageData.getString("CODE").equals(value)){
                    value = pageData.getString("NAME");
                }
            }

        }else if ("zhuyaohangye".equals(key)){
            if (Optional.ofNullable(value).isPresent() && !"".equals(value)){
                String[] split = value.split(",");
                StringBuilder stringBuilder = new StringBuilder();
                for (String code:split){
                    for (PageData pageData:codeByName){
                        if (pageData.getString("CODE").equals(code)){
                            String s = pageData.getString("NAME");
                            stringBuilder.append(s).append(",");
                        }
                    }
                }
                String s = stringBuilder.toString();
                s = s.substring(0,s.length()-1);
                value = s;
            }
        }

        return value;
    }


}
